/**
 * Created by Administrator on 2018/5/27 0027.
 */
//easyui  验证框重写

//删除chanpin
function shanchu() {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $.messager.confirm('Confirm', '是否确认删除' + row.kehuming + '?', function (r) {
            if (r) {
                $.ajax({
                    url: path + '/pifashang/del.do',
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        id: row.id
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: "删除" + row.kehuming + '成功!',
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $("#dg").datagrid({
                                url: path + '/pifashang/list.do'
                            });
                        } else {
                            $.messager.alert('提示', data.msg);
                        }
                    }
                })
            }
        });
    } else {
        $.messager.alert('提示', '请选择要操作的数据');
    }
}
//导出excel
function downExcel() {
    window.location.href = path + "/pifashang/downloadexcel.xls?kehuming=" + $("#kehumingcheng").searchbox("getValue") + "&shengfen=" + $("#shengfen").combobox("getValue");
}


//打开窗口
function openWindow(code) {
    clear();
    if (code == 2) {
        $('#addwindow').dialog({title: "修改批发商"});

        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.ajax({
                url: path + '/pifashang/find/id.do',
                dataType: 'json',
                type: 'POST',
                data: {
                    id: row.id
                },
                success: function (data) {
                    if (data.code == 200) {
                        $("#id").val(data.rows.id)
                        $("#kehuming").textbox("setValue", data.rows.kehuming);
                        $("#phone").textbox("setValue", data.rows.phone);
                        $("#shengfenwindow").combobox("setValue", data.rows.shengfen);
                        $("#address").textbox("setValue", data.rows.address);
                        $("#dianming").textbox("setValue", data.rows.dianming);
                        $("#shouxinedu").numberbox("setValue", data.rows.shouxinedu);
                        $("#zhekou").textbox("setValue", data.rows.pifaZhekou);
                        $("#addwindow").window('open');
                    } else {
                        $.messager.alert('提示', data.msg);
                    }
                }
            })
        } else {
            $.messager.alert('提示', '请选择要操作的数据');
            return;
        }
    } else {
        $("#id").val("")
        $('#addwindow').dialog({title: "新增批发商"});
        $("#addwindow").window('open');
    }

}
//保存款号信息
function save() {
    var kehuming = $("#kehuming").val().trim();
    if (kehuming == "" || kehuming == null) {
        $("#kehumingtest").text('*请输入客户名称');
        return;
    } else {
        $("#kehumingtest").text('');
    }
    var shengfen = $("#shengfenwindow").combobox("getValue");
    if (shengfen == -1) {
        $("#shengfentest").text('*请选择省份');
        return;
    } else {
        $("#shengfentest").text('');
    }
    var shouxinedu = $("#shouxinedu").numberbox("getValue");
    if (shouxinedu == "" || shouxinedu == null) {
        $("#shouxinedutest").text('*请输入授信额度');
        return;
    } else {
        $("#shouxinedutest").text('');
    }
    var pifaZhekou = $("#zhekou").val();
    if (!/^(0.\d+|1)$/.test(pifaZhekou)) {
        $("#zhekoutest").text('*请正确输入默认折扣');
        return;
    } else {
        $("#zhekoutest").text('');
    }
    $.ajax({
        url: path + '/pifashang/save.do',
        dataType: 'json',
        type: 'POST',
        data: {
            id: $("#id").val(),
            kehuming: kehuming,
            phone: $("#phone").val(),
            dianming: $("#dianming").val().trim(),
            shengfen: shengfen,
            shouxinedu: shouxinedu,
            address: $("#address").val(),
            pifaZhekou: pifaZhekou
        },
        success: function (data) {
            if (data.code == 200) {
                var sts = "";
                if ($("#id").val().trim() != "" && $("#id").val().trim() != null) {
                    sts = '修改';
                } else {
                    sts = '添加';
                }
                $.messager.show({
                    title: '提示',
                    msg: sts + kehuming + '成功!',
                    timeout: 3000,
                    showType: 'slide'
                });
                $("#dg").datagrid({
                    url: path + '/pifashang/list.do'
                })
                ;
                $("#addwindow").window('close');
            } else {
                $.messager.alert('警告', data.msg);
            }
        }
    })
}
//清除所有值
function clear() {
    $("#id").val("")
    $("#kehuming").textbox("setValue", "");
    $("#shengfenwindow").combobox("setValue", "-1");
    $("#dianming").textbox("setValue", "");
    $("#zhekou").textbox("setValue", "");
    $("#phone").textbox("setValue", "");
    $("#shouxinedu").numberbox("setValue", "");
    $("#address").textbox("setValue", "");
    $("#kehumingtest").text('');
    $("#shouxinedutest").text('');
    $("#shengfentest").text("")
}
